/**
* @author:kellen
* @contact:xiekellen@hotmail.com
* @company:achivo-gz
* @created on:
* @modified on:
*/

package com.sasis.dao.ibatis;

import java.util.List;

import com.sasis.dao.ibatis.BaseDaoiBATIS;
import com.sasis.model.Flight;
import com.sasis.dao.FlightDao;

import org.springframework.orm.ObjectRetrievalFailureException;

public class FlightDaoiBatis extends BaseDaoiBATIS implements FlightDao {

    /**
     * @see com.sasis.dao.FlightDao#getFlights(com.sasis.model.Flight)
     */
    public List getFlights(final Flight flight) {
          return getSqlMapClientTemplate().queryForList("getFlights", flight);
    }

    /**
     * @see com.sasis.dao.FlightDao#getFlight(Integer id)
     */
    public Flight getFlight(Integer id) {
        Flight flight = (Flight) getSqlMapClientTemplate().queryForObject("getFlight", id);

       if (flight == null) {
            throw new ObjectRetrievalFailureException(Flight.class, id);
        }

        return flight;
    }
    
    public Flight getFlight(final Flight flightVal) {
        Flight flight = (Flight) getSqlMapClientTemplate().queryForObject("getFlight", flightVal);

        //if (flight == null) {
            //throw new ObjectRetrievalFailureException(Flight.class, flightVal);
      //  }

        return flight;
    }

    /**
     * @see com.sasis.dao.FlightDao#saveFlight(Flight flight)
     */    
    public void saveFlight(final Flight flight) {
        Integer id = flight.getId();
        // check for new record
        if (id == null) {
            id = (Integer) getSqlMapClientTemplate().insert("addFlight", flight);
        } else {
            getSqlMapClientTemplate().update("updateFlight", flight);
        }
        if( id == null ) {
            throw new ObjectRetrievalFailureException(Flight.class, id);
        }
    }

    /**
     * @see com.sasis.dao.FlightDao#removeFlight(Integer id)
     */
    public void removeFlight(Flight flight) {
        getSqlMapClientTemplate().update("deleteFlight",  flight);
    }
}
